{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, TypeFamilies #-}

module Survey.TWOMASS
 ( TWOMASS(..)
 , JHK
 ) where


import Types
import Survey
import Helper           ( isZero )
import ObsData          ( ObsData(..) )
import Model.Photometry ( ModelPhotometry(..) )

import Data.AdditiveGroup (AdditiveGroup(..))
import Control.Monad.Reader ( asks )

-- Обзор
data TWOMASS = TWOMASS deriving Show

-- TWOMASS
instance ModelPhotometry (JH TWOMASS) where
  mag = do
     j <- asks $ j_twomass . response
     h <- asks $ h_twomass . response

     quad <- asks $ quad . integrator

     let val f = JH TWOMASS $ v
           where
             a = quad $ j * f
             b = quad $ h * f

             v = if isZero a || isZero b 
                  then Nothing 
                  else Just $ -2.5 * log $ a/b
     return val


instance ModelPhotometry (JK TWOMASS) where
  mag = do
     j <- asks $ j_twomass . response
     k <- asks $ k_twomass . response

     quad <- asks $ quad . integrator

     let val f = JK TWOMASS $ v
           where
             a = quad $ j * f
             b = quad $ k * f

             v = if isZero a || isZero b 
                  then Nothing 
                  else Just $ -2.5 * log $ a/b
     return val


-- JHK обзора 2MASS
type JHK = (J TWOMASS, H TWOMASS, K TWOMASS)

instance ObsData JHK where
  type Data JHK = (JH TWOMASS, JK TWOMASS)

  toInitalData ((J TWOMASS j), (H TWOMASS h), (K TWOMASS k)) = return (JH TWOMASS (j-h), JK TWOMASS (j-k))

  toModelData _ = mag

-- Standard instance for an applicative functor applied to a vector space.
instance  AdditiveGroup (JH TWOMASS) where
  zeroV = JH TWOMASS $ Nothing 
  (^+^) (JH _ a) (JH _ b)  = JH TWOMASS $ a+b
  negateV (JH _ a) = JH TWOMASS (-a)

instance  AdditiveGroup (JK TWOMASS) where
  zeroV = JK TWOMASS $ Nothing 
  (^+^) (JK _ a) (JK _ b)  = JK TWOMASS $ a+b
  negateV (JK _ a) = JK TWOMASS (-a)